package fun.boomcat.luckyhe.mirai.plugin.botcontrol.util;

import net.mamoe.mirai.console.command.CommandSender;
import net.mamoe.mirai.contact.NormalMember;

import java.util.Arrays;

/**
 * @author Lucky_He
 * @date 2021年8月20日 0:08
 * @description 操作群成员相关
 */
public class GroupMemberOperation {

    public static void groupMemberOperation(CommandSender commandSender, Object[] args, NormalMember member, String primaryName) {
//        群成员操作相关
        int len = args.length;
        String operation = args[0].toString();
        switch (operation.toLowerCase()) {
            case "help":
            case "帮助":

                commandSender.sendMessage(ControlCommandHelp.groupMemberOperationHelp(primaryName, member));
                break;

            case "namecard":
            case "群昵称":

                groupMemberChangeNameCard(
                        commandSender,
                        Arrays.copyOfRange(args, 1, len),
                        member
                );
                break;

            case "specialtitle":
            case "头衔":

                groupMemberChangeSpecialTitle(
                        commandSender,
                        Arrays.copyOfRange(args, 1, len),
                        member
                );
                break;

            case "mute":
            case "禁言":

                groupMemberMute(
                        commandSender,
                        Arrays.copyOfRange(args, 1, len),
                        member
                );
                break;

            case "unmute":
            case "解除禁言":

                groupMemberUnmute(
                        commandSender,
                        member
                );
                break;

            case "kick":
            case "移除":

                groupMemberKick(
                        commandSender,
                        Arrays.copyOfRange(args, 1, len),
                        member
                );
                break;

            case "setadmin":
            case "设为管理":

                groupMemberSetAdmin(
                        commandSender,
                        member
                );

                break;

            case "unsetadmin":
            case "取消管理":

                groupMemberUnsetAdmin(
                        commandSender,
                        member
                );
                break;

            case "send":
            case "发送":

                groupMemberSend(
                        commandSender,
                        Arrays.copyOfRange(args, 1, len),
                        member
                );
                break;

            default:

                commandSender.sendMessage(ControlCommandHelp.groupMemberOperationHelp(primaryName, member));
                break;
        }
    }

    public static void groupMemberChangeNameCard(CommandSender commandSender, Object[] args, NormalMember member) {
//        修改群昵称操作
        String formerNameCard = member.getNameCard();

        StringBuilder groupNameCard = new StringBuilder();
        for (Object gnc : args) {
            groupNameCard.append(gnc).append(" ");
        }

        try {
            member.setNameCard(groupNameCard.toString().trim());
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("已将群" + member.getGroup().getId() +
                "成员" + member.getId() + "的群昵称从" + formerNameCard + "修改为" + groupNameCard.toString().trim());

    }

    public static void groupMemberChangeSpecialTitle(CommandSender commandSender, Object[] args, NormalMember member) {
//        修改群头衔操作
        String formerSpecialTitle = member.getSpecialTitle();

        StringBuilder specialTitle = new StringBuilder();
        for (Object st : args) {
            specialTitle.append(st).append(" ");
        }

        try {
            member.setSpecialTitle(specialTitle.toString().trim());
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("已将群" + member.getGroup().getId() +
                "成员" + member.getId() + "的群头衔从" + formerSpecialTitle + "修改为" + specialTitle.toString().trim());
    }

    public static void groupMemberMute(CommandSender commandSender, Object[] args, NormalMember member) {
//        禁言操作
        int len = args.length;
        if (len == 0) {
            commandSender.sendMessage("缺少禁言秒数参数，请在该指令后接禁言秒数");
            return;
        }

        String muteSecStr = args[0].toString();
        int muteSec;
        try {
            muteSec = Integer.parseInt(muteSecStr);
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        try {
            member.mute(muteSec);
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }
        commandSender.sendMessage("已将群" + member.getGroup().getId() +
                "成员" + member.getId() + "禁言" + muteSec + "秒");
    }

    public static void groupMemberUnmute(CommandSender commandSender, NormalMember member) {
//        解除禁言操作
        try {
            member.unmute();
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("已将群" + member.getGroup().getId() +
                "成员" + member.getId() + "解除禁言");
    }

    public static void groupMemberKick(CommandSender commandSender, Object[] args, NormalMember member) {
//        踢除群成员
        StringBuilder attachMsg = new StringBuilder();
        for (Object arg : args) {
            attachMsg.append(arg.toString()).append(" ");
        }

        try {
            member.kick(attachMsg.toString().trim());
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("已将群" + member.getGroup().getId() +
                "成员" + member.getId() + "移除");
    }

    public static void groupMemberSetAdmin(CommandSender commandSender, NormalMember member) {
//        设为管理
        try {
            member.modifyAdmin(true);
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("已将群" + member.getGroup().getId() +
                "成员" + member.getId() + "设为管理");
    }

    public static void groupMemberUnsetAdmin(CommandSender commandSender, NormalMember member) {
//        取消管理
        try {
            member.modifyAdmin(false);
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("已将群" + member.getGroup().getId() +
                "成员" + member.getId() + "管理权限移除");
    }

    public static void groupMemberSend(CommandSender commandSender, Object[] args, NormalMember member) {
//        发送私聊消息
        StringBuilder msg = new StringBuilder();
        for (Object arg : args) {
            msg.append(arg.toString()).append(" ");
        }

        try {
            member.sendMessage(msg.toString().trim());
        } catch (Exception e) {
            commandSender.sendMessage("错误：" + e.toString());
            return;
        }

        commandSender.sendMessage("已向群" + member.getGroup().getId() +
                "成员" + member.getId() + "发送消息：\n" + msg.toString().trim());
    }
}
